package com.zhaiweijia.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhaiweijia.entity.Article;
import com.zhaiweijia.entity.User;
import com.zhaiweijia.service.ArticleService;
import com.zhaiweijia.service.UserService;

@Controller
@RequestMapping("admin")
public class AdminController {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private UserService userService;
	
	
	//跳转管理员中心
	@RequestMapping({"/","","index"})
	public String index() {
		
		return "admin/index";
	}
	
	
	
	@RequestMapping("updateLocked")
	@ResponseBody
	public boolean updateLocked(User user) {
		try {			
			userService.updateLocked(user);			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	/**
	 * 用户管理
	 * @param model
	 * @param user
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("users")
	public String users(Model model,User user,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize) {
		//如果status==2
		if(user.getLocked() !=null && user.getLocked()==2 ) {
			user.setLocked(null);
		}
		
		//查询数据
		PageInfo  info = userService.list(pageNum,pageSize,user);
		
		model.addAttribute("info", info);
		model.addAttribute("user", user);
		
		return "admin/users";
		
	}
	
	/**
	 * 修改文章状态
	 * @param article
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Article article) {
		try {
			
			articleService.update(article);
			
			return true;
		} catch (Exception e) {
			return false;
		}
		
		
	}
	/**
	 * 列表
	 * @param model
	 * @param article
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("articles")
	public String articles(Model model,Article article,@RequestParam(defaultValue = "1") Integer pageNum,
											@RequestParam(defaultValue = "3") Integer pageSize) {
		//如果status==2
		if(article.getStatus() !=null && article.getStatus()==2 ) {
			article.setStatus(null);
		}
		
		//查询数据
		PageInfo  info = articleService.list(pageNum,pageSize,article);
		
		model.addAttribute("info", info);
		model.addAttribute("article", article);
		return "admin/articles";
		
	}
}
